package com.atguigu.yygh.hosp.testmongo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/mongo2")
public class TestMongo2 {
    @Autowired
    private UserRepository userRepository;

    //添加
    @GetMapping("create")
    public void createUser() {
        User user = new User();
        user.setAge(20);
        user.setName("张三");
        user.setEmail("3332200@qq.com");
        User user1 = userRepository.save(user);
        System.out.println(user1);
    }
    //查询所有
    @GetMapping("findAll")
    public void findAll(){
        List<User> userList = userRepository.findAll();
        System.out.println(userList);
    }

    //根据id查询
    @GetMapping("findId")
    public void findId(){
        User user = userRepository.findById("610e456b3dd73519b3086958").get();
        System.out.println(user);
    }
}
